package spring.cloud.tasks.tasks_manager.tasks_manager.listener;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;

import java.nio.charset.StandardCharsets;

@Slf4j
@Setter
@Getter
public abstract class TreeCacheListener implements org.apache.curator.framework.recipes.cache.TreeCacheListener {

    @Override
    public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
        ChildData childData = treeCacheEvent.getData();
        if (childData != null) {
            TreeCacheEvent.Type type = treeCacheEvent.getType();
            String path = childData.getPath();
            String nodeData = null;
            byte[] bytes = childData.getData();
            if (bytes != null) {
                nodeData = new String(bytes, StandardCharsets.UTF_8);
            }
            if (path != null) {
                childEvent(type, path, nodeData);
            }
        }
    }

    public abstract void childEvent(TreeCacheEvent.Type type, String path, String nodeData) throws Exception;

}
